﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ModelPredictPro.General.IO;
using System.IO;

namespace ModelPredictPro.GeneralTest {
    [TestFixture]
    public class TestFileHelper {
        static string[] lines = "1234567890".ToCharArray().Select(o => o.ToString()).ToArray();

        [Test]
        public void TestNDivide() {
            TempFileInfo source = new TempFileInfo(lines);
            FileInfo[] nfiles = FileHelper.DivideToNFiles(source.FilePath, 5);
            for (int i = 0; i < nfiles.Length; i++) {
                FileInfo item = nfiles[i];
                Console.WriteLine("[{0}] {1}", i+1, item.FullName );
                foreach (string line in FileReader.ReadAllLines(item.FullName)) {
                    Console.WriteLine(line);
                }
            }
            // 测试文件合并功能
            string[] sourceFiles = nfiles.Select(o => o.FullName).ToArray();
            source.Delete();
            FileHelper.ImergeFiles(source.FilePath, sourceFiles);
            Console.WriteLine("合并生成的文件");
            foreach (string line in FileReader.ReadAllLines(source.FilePath)) {
                Console.WriteLine(line);
            }

            source.Delete();
            nfiles.ToList().ForEach(o => o.Delete());
        }

        static string []postiveSet = "1234567890123".ToCharArray().Select(o => string.Format("Postive:{0}", o)).ToArray();
        static string []negtiveSet = "1234567890123".ToCharArray().Select(o => string.Format("Negative:{0}", o)).ToArray();

        [Test]
        public void TestNFold() {
            TempFileInfo pos = new TempFileInfo(postiveSet);
            TempFileInfo neg = new TempFileInfo(negtiveSet);
            string[] trainFiles, testFiles;
            trainFiles = testFiles = null;
            FileHelper.GenerateNFoldFiles(pos.FilePath, neg.FilePath, 5, ref trainFiles, ref testFiles);
            int count = 1;
            foreach (string item in trainFiles) {
                Console.WriteLine("[{0}] {1}", count++, item );
                foreach (string line in FileReader.ReadAllLines(item)) {
                    Console.WriteLine(line);
                }
            }
            count = 1;
            foreach (string item in testFiles) {
                Console.WriteLine("[{0}] {1}", count++, item );
                foreach (string line in FileReader.ReadAllLines(item)) {
                    Console.WriteLine(line);
                }
            }
            trainFiles.ToList().ForEach(o => File.Delete(o));
            testFiles.ToList().ForEach(o => File.Delete(o));
        }
    }
}
